﻿using System;
using System.Data;
using ZX.CRM.Library;
using ZX.CRM.Bussiness;
namespace ZX.WebApp.Crm.BasePages
{
    public partial class FormsPrint : System.Web.UI.Page
    {
        ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient client = new ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient();

        public string workflowInsId
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BaseUserPage.LoadWaitPage();
            }
            if (Request["OperatorInstanceId"] != null)
            {
                string OInsId = Request["OperatorInstanceId"].ToString();
                InitData(OInsId);
            }
            //InitData("d2ba113f-383e-4bdf-b278-fed705af9933");

        }
        public void Init(string workflowInsId)
        {
            DataTable dt = client.GetWorkflowdetail(workflowInsId);
            gvworkflowMessage.DataSource = dt;
            gvworkflowMessage.DataBind();
        }

        public string URL
        {
            get;
            set;
        }
        /// <summary>
        /// 流程ID
        /// </summary>
        public string WorkFlowId
        {
            get;
            set;
        }
        /// <summary>
        /// 流程务任ID
        /// </summary>
        public string WorkTaskId
        {
            get;
            set;
        }
        /// <summary>
        /// 表单类型
        /// </summary>
        public string FormType
        {
            get;
            set;
        }


        /// <summary>
        /// 流程实例ID
        /// </summary>
        public string WorkFlowInsId
        {
            get;
            set;
        }
        /// <summary>
        /// 流程任务实例ID
        /// </summary>
        public string WorkTaskInsId
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public string OperatorInsId
        {
            get;
            set;
        }
        /// <summary>
        /// 流程状态
        /// </summary>
        public string Status
        {
            get;
            set;
        }
        private void InitData(string OInsId)
        {

          
                OperatorInsId =OInsId;// Request["OperatorInstanceId"].ToString();
                DataTable dt = client.GetOperatorInstanceView(OperatorInsId);
                bool islock = true;

                if (dt != null && dt.Rows.Count > 0)
                {
                    islock = client.IsLock(dt.Rows[0]["WorkTaskInstanceId"].ToString());
                    WorkFlowId = dt.Rows[0]["WorkflowId"].ToString();

                    WorkTaskId = dt.Rows[0]["WorktaskId"].ToString();
                    if (dt.Rows[0]["flStatus"].ToString() == "3")
                    {
                        Status = dt.Rows[0]["flStatus"].ToString();
                    }
                    else if (islock == false)
                    {
                        Status = "2";
                    }
                    else
                    {
                        Status = dt.Rows[0]["Status"].ToString();
                    }
                    WorkFlowInsId = dt.Rows[0]["WorkFlowInstanceId"].ToString();
                    WorkTaskInsId = dt.Rows[0]["WorkTaskInstanceId"].ToString();
                    Init(WorkFlowInsId);
                    DataTable cdt = client.GetWorkTaskFormsView(WorkTaskId);
                    if (cdt != null && cdt.Rows.Count > 0)
                    {
                        FormType = cdt.Rows[0]["FormType"].ToString();
                        if (FormType == "Html")
                        {
                            DataTable tdt = client.GetTableEntity(cdt.Rows[0]["FilePath"].ToString());
                            string entityName = tdt.Rows[0]["MasterTableName"].ToString();
                            string formid = tdt.Rows[0]["FormID"].ToString();

                            string id = ReturnPayId(WorkFlowInsId, entityName);

                            if (Status == "3")
                            {
                                DataTable netTaskins = client.GetNetTaskFormID(WorkTaskInsId, WorkTaskId);
                                if (netTaskins.Rows.Count > 0)
                                {
                                    WorkTaskId = netTaskins.Rows[0]["workTaskId"].ToString();
                                    formid = netTaskins.Rows[0]["FilePath"].ToString();
                                }
                            }


                            URL = "../../Extend/AutoLoadForm.aspx?entity=" + entityName + "&action=Select&formid=" + formid + "&id=" + id + "&workflowid=" + WorkFlowId + "&worktaskid=" + WorkTaskId + "&statues=" + Status;
                            //powerButton();//根据控制权限创建的按钮

                        }


                    }
                }
            }



        /// <summary>
        /// 返回业务表单ID   
        /// </summary>
        /// <param name="cpid"></param>
        /// <returns></returns>
        public string ReturnPayId(string cpid, string entity)
        {
            BCrmPayFlow bCrmPayFlow = new BCrmPayFlow();
            string vcp = bCrmPayFlow.GetTableInfoById(cpid == "" ? Guid.NewGuid() : new Guid(cpid), entity);
            if (vcp != null)
            {
                return vcp.ToString();
            }
            return "";
        }



       
    }
}